//
//  TableView.swift
//  foundationDemo
//
//  Created by Zhao Mingzhi on 16/6/28.
//  Copyright © 2016年 HNA. All rights reserved.
//

import Foundation
import UIKit

class ScrollView:UIViewController,UIScrollViewDelegate{
    var scrollview:UIScrollView!
    var pageControl:UIPageControl!
    var view1:UIView!
    var view2:UIView!
    var view3:UIView!
    var viewWidth:CGFloat = 0.0
    var viewHeight:CGFloat = 0.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewHeight = self.view.frame.size.height
        viewWidth = self.view.frame.size.width
        
        self.scrollview = UIScrollView(frame: self.view.frame)
        self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)
        
        self.scrollview.pagingEnabled = true
        self.scrollview.delegate = self
        
        self.view1 = UIView(frame: self.view.frame)
        self.view1.backgroundColor = UIColor.blackColor()
        self.view1.frame = CGRectMake(0.0, 0.0, viewWidth, viewHeight)
        self.scrollview.addSubview(view1)
        
        self.view2 = UIView(frame: self.view.frame)
        self.view2.backgroundColor = UIColor.blueColor()
        self.view2.frame = CGRectMake(viewWidth, 0.0, viewWidth, viewHeight)
        self.scrollview.addSubview(view2)
        
        self.view3 = UIView(frame: self.view.frame)
        self.view3.backgroundColor = UIColor.greenColor()
        self.view3.frame = CGRectMake(viewWidth*2, 0.0, viewWidth, viewHeight)
        self.scrollview.addSubview(view3)
        
        var pageControlHeight:CGFloat = 38.0
        var pageControlWidth:CGFloat = 120.0
        let pageControlFrame = CGRectMake((viewWidth-pageControlWidth)/2, (viewHeight-pageControlHeight), pageControlWidth, pageControlHeight)
        
        self.pageControl = UIPageControl(frame:pageControlFrame)
        self.pageControl.backgroundColor = UIColor.redColor()
        self.pageControl.alpha=0.5
        self.pageControl.numberOfPages = 3
        self.pageControl.currentPage = 0
        
        self.pageControl.addTarget(self, action: #selector(ScrollView.changePage(_:)), forControlEvents: UIControlEvents.ValueChanged)
        view.addSubview(scrollview)
        self.view.addSubview(self.pageControl)
        
        
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
        var offset = scrollview.contentOffset
        self.pageControl.currentPage = Int(offset.x)/Int(viewWidth)
    }
    func changePage(sender:AnyObject){
        UIView.animateWithDuration(0.3, animations: {
            var whichPage = self.pageControl.currentPage
            self.scrollview.contentOffset=CGPointMake(self.viewWidth*CGFloat(whichPage), 0)
        })
    }
    
    
    
    
}